package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 允许任何来源的跨域请求，并允许携带cookie和任何请求头，支持所有的HTTP方法
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); //允许白名单域名进行跨域调用
        corsConfiguration.setAllowCredentials(true); //允许跨域发送cookie
        corsConfiguration.addAllowedHeader("*"); //放行所有原始头信息
        corsConfiguration.addAllowedMethod("*"); ////允许所有请求方法跨域调用

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);

        CorsFilter corsFilter = new CorsFilter(source);
        return corsFilter;
    }
}
